package net.kunyv.application;

import com.sun.javafx.robot.impl.FXRobotHelper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import net.kunyv.DAL.GenEntityBean;
import net.kunyv.DAL.MySqlInitBeanImpl;
import net.kunyv.DAL.OracleInitBeanImpl;
import net.kunyv.DAL.SqlServerInitBeanImpl;
import net.kunyv.entity.DaoEntity;
import net.kunyv.entity.DbConfig;
import net.kunyv.entity.EntityColumn;
import net.kunyv.entity.XmlPojo;
import net.kunyv.factory.DataSourceStrategyFactory;
import net.kunyv.util.ColumnUtil;
import net.kunyv.util.PrintStackTrace;
import net.kunyv.util.XMLUtil;
import org.dom4j.Document;
import org.dom4j.Element;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

public class MainController implements Initializable {
  @FXML
  public Button nextBt;
  @FXML
  public TextField usernameTxt;
  @FXML
  public TextField tablenameTxt;
  @FXML
  public TextField pwdTxt;
  @FXML
  public TextField aliasTxt;
  @FXML
  public TextField urlTxt;
  @FXML
  public TextField packageTxt;
  @FXML
  public TextField widthTxt;
  @FXML
  public TextField suffixTxt;
  @FXML
  public ComboBox databaseCmb;
  @FXML
  public ComboBox projectCmb;
  @FXML
  public TextArea customsqlTxtA;
  @FXML
  public AnchorPane MainFX;
  @FXML
  public CheckBox customChk;

  public MainController() {
  }

  public void initialize(URL location, ResourceBundle resources) {
    this.customsqlTxtA.setStyle("-fx-control-inner-background:#EEEEEE");
    this.projectCmb.setItems(this.getProjectCmbList());
    this.databaseCmb.setItems(FXCollections.observableArrayList(new String[]{"ORACLE", "MySQL","Sqlserver"}));
    DataSourceStrategyFactory.register("ORACLE", new OracleInitBeanImpl());
    DataSourceStrategyFactory.register("MySQL", new MySqlInitBeanImpl());
      DataSourceStrategyFactory.register("Sqlserver", new SqlServerInitBeanImpl());
  }

  public void next() {
    try {
      StringBuffer alertStr = new StringBuffer("");
      if (this.urlTxt.getText().equals("")) {
        alertStr.append("               连接串不能为空\r\n");
      }

      if (this.usernameTxt.getText().equals("")) {
        alertStr.append("               用户名不能为空\r\n");
      }

      if (String.valueOf(this.pwdTxt.getText()).equals("")) {
        alertStr.append("               密码不能为空\r\n");
      }

      if (this.tablenameTxt.getText().equals("")) {
        alertStr.append("               表名不能为空\r\n");
      }

      if (this.customChk.isSelected() && this.customsqlTxtA.getText().equals("")) {
        alertStr.append("               自定义sql不能为空\r\n");
      }

      if (!alertStr.toString().equals("")) {
        Alert alert = new Alert(AlertType.INFORMATION, alertStr.toString(), new ButtonType[0]);
        alert.setTitle("信息");
        alert.setHeaderText("提示");
        alert.show();
        alertStr = null;
        return;
      }

      DaoEntity daoentity = this.getDaoEntity();
      DbConfig dbconfig = this.getDbConfig();
      List<EntityColumn> list = (new GenEntityBean()).GenBean(dbconfig, daoentity);
      ObservableList<Stage> stage = FXRobotHelper.getStages();
      FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("columnApp.fxml"));
      Parent root = (Parent)fxmlLoader.load();
      ColumnController controller = (ColumnController)fxmlLoader.getController();
      controller.setColumnList(list);
      controller.setDaoentity(daoentity);
      controller.initializeFx();
      Scene scene = new Scene(root, 1200.0D, 750.0D);
      Stage secondWindow = new Stage();
      secondWindow.setScene(scene);
      secondWindow.setMaximized(true);
      secondWindow.show();
    } catch (Exception var11) {
      PrintStackTrace.getStackLabel(var11);
    }

  }

  public void changeproject(Event event) {
    if (this.projectCmb.getSelectionModel().getSelectedItem() != null) {
      XmlPojo xp = (XmlPojo)this.projectCmb.getSelectionModel().getSelectedItem();
      this.usernameTxt.setText(xp.getUsername());
      this.pwdTxt.setText(xp.getPassword());
      this.urlTxt.setText(xp.getUrl());
      this.packageTxt.setText(xp.getPackageName());
      this.databaseCmb.setValue(xp.getDatabase());
    }

  }

  public void checkCustom() {
    if (this.customChk.isSelected()) {
      this.customsqlTxtA.setEditable(true);
      this.customsqlTxtA.setStyle("-fx-control-inner-background:#FFFFFF");
    } else {
      this.customsqlTxtA.setEditable(false);
      this.customsqlTxtA.setStyle("-fx-control-inner-background:#EEEEEE");
    }

  }

  private ObservableList getProjectCmbList() {
    InputStream is = null;
    ObservableList observableList = null;

    try {
      is = new FileInputStream(System.getProperty("user.dir") + "/src/database.xml");
    } catch (Exception var10) {
      var10.printStackTrace();
    }

    if (is != null) {
      Document d = XMLUtil.readXML(is);
      Element element = d.getRootElement();
      List<Element> list = element.elements();
      List<XmlPojo> resultlist = new ArrayList();
      Iterator var7 = list.iterator();

      while(var7.hasNext()) {
        Element e = (Element)var7.next();
        XmlPojo xp = new XmlPojo();
        xp.setUsername(XMLUtil.getChildElement(e, "username").getText());
        xp.setDirectory(XMLUtil.getChildElement(e, "saveDir").getText());
        xp.setPackageName(XMLUtil.getChildElement(e, "packageName").getText());
        xp.setPassword(XMLUtil.getChildElement(e, "password").getText());
        xp.setUrl(XMLUtil.getChildElement(e, "url").getText());
        xp.setProjectName(XMLUtil.getChildElement(e, "projectName").getText());
        xp.setDatabase(XMLUtil.getChildElement(e, "database").getText());
        resultlist.add(xp);
      }

      observableList = FXCollections.observableList(resultlist);
    }

    return observableList;
  }

  private DaoEntity getDaoEntity() {
    DaoEntity daoentity = new DaoEntity();
    daoentity.setTableName(this.tablenameTxt.getText().replaceAll(" ", "").toUpperCase());
    if (this.customChk.isSelected()) {
      daoentity.setCustomSql(true);
    } else {
      daoentity.setCustomSql(false);
    }

    if (this.databaseCmb.getSelectionModel().getSelectedItem().toString().equals("MySQL")) {
      daoentity.setMybatis(true);
    } else {
      daoentity.setMybatis(false);
    }

    String width;
    if (this.aliasTxt.getText().equals("")) {
      width = ColumnUtil.genClassAndMethodName(daoentity.getTableName(), true, daoentity.isMybatis());
      daoentity.setEntityName(width);
    } else {
      daoentity.setEntityName(this.aliasTxt.getText());
    }

    daoentity.setEntityNameAlias(ColumnUtil.lowerCase(daoentity.getEntityName()));
    daoentity.setSql(this.customsqlTxtA.getText());
    daoentity.setPackageName(this.packageTxt.getText());
    daoentity.setDatabaseName(this.databaseCmb.getSelectionModel().getSelectedItem().toString());
    daoentity.setPackageNameSuffix(this.suffixTxt.getText());
    width = this.widthTxt.getText();
    daoentity.setWidthText(width.equals("") ? 320 : Integer.parseInt(width));
    return daoentity;
  }

  private DbConfig getDbConfig() {
    DbConfig dbconfig = new DbConfig();
    String databaseName = this.databaseCmb.getSelectionModel().getSelectedItem().toString();
    if (databaseName.equals("ORACLE")) {
      dbconfig.setDriverClass("oracle.jdbc.OracleDriver");
    }

    if (databaseName.equals("MySQL")) {
      dbconfig.setDriverClass("com.mysql.jdbc.Driver");
    }

    if (databaseName.equals("Sqlserver")) {
      dbconfig.setDriverClass("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    }

    dbconfig.setPassword(String.valueOf(this.pwdTxt.getText()));
    dbconfig.setUrl(this.urlTxt.getText());
    dbconfig.setUsername(this.usernameTxt.getText());
    return dbconfig;
  }
}
